package com.example.demo.web;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import javax.validation.Valid;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.Errors;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;

import com.example.demo.domain.Final;
import com.example.demo.domain.Person;

import lombok.extern.slf4j.Slf4j;

@Slf4j
@Controller
@RequestMapping("/submit")
public class SumbitController {
	@GetMapping
	public String showDesignForm(Model model) {
		model.addAttribute("submit",new Person());
		return "submit";

	}

	@PostMapping//����POST����,@ModelAttributeע���Զ���model�е�����ע�뵽���εĲ���
	public String processOrder(@Valid Final submit,Errors errors) {
		 if(errors.hasErrors()) {
			 return "submit";
		 }
	   log.info("submit submitted: " + submit);
	   return "redirect:/";
	 }
	@ModelAttribute
	public void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        HttpSession session = request.getSession();
        String name=(String)session.getAttribute("name");
        String number=(String)session.getAttribute("number");
        String year=(String)session.getAttribute("year");
        String major=(String)session.getAttribute("major");
        String college=(String)session.getAttribute("college");
        String[] value= (String[]) session.getAttribute("data");
        if(value==null || value.length==0){
            out.write("你没有选择任何课程!!");
            return;
        }
        
        //显示用户买过的商品
        out.write(name+"<br>");
        out.write(number+"<br>");
        out.write(year+"<br>");
        out.write(major+"<br>");
        out.write(college+"<br>");
        out.write("已选择课程:"+"<br>");
        for(String sourse: value){
            out.write(sourse + "<br/>");
        }
    }
}
